package com.alks.function.service.serviceImpl;

import com.alks.common.aop.AutoPageAop;
import com.alks.common.data.thread.UserIdThread;
import com.alks.common.utils.beanutils.StringUtils;
import com.alks.common.utils.stringUtils.ZStringUtils;
import com.alks.entity.data.entity.PcLotDetail;
import com.alks.entity.data.entity.PcMtlCheck2;
import com.alks.entity.data.entity.PcSlipDetailPartBf2;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.entity.exception.ServiceErrorException;
import com.alks.function.data.dto.PcSlipDetailSoleReq;
import com.alks.function.mapper.PcSlipDetailSoleMapper;
import com.alks.function.mapper.finance.costcheck.PcSlipDetailSoleVo;
import com.alks.function.mapper.stockdept.pcfactorystockquery.SD_SQ_PcMtlCheckMapper;
import com.alks.function.service.IPcSlipDetailSoleService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.MathContext;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-05-24
 */
@Service
public class PcSlipDetailSoleServiceImpl implements IPcSlipDetailSoleService {
    @Autowired
    private PcSlipDetailSoleMapper pcSlipDetailSoleMapper;

    @Override
    @AutoPageAop
    public ResponseInfo getSoleCheck(PcSlipDetailSoleReq req) {
        Map<String, Object> map = new HashMap<>();
        req.setCompanyId(UserIdThread.get().getCompanyId());
        List<PcSlipDetailSoleVo> list = pcSlipDetailSoleMapper.getSoleCheck(req);
        Page<PcSlipDetailSoleVo> page = (Page<PcSlipDetailSoleVo>) list;
        map.put("list", list);
        map.put("total", page.getTotal());
        return ResponseInfo.ok(map);
    }

    @Override
    public ResponseInfo optionSoleCheck(List<String> slipNos, String check) {
        for (String slipNo : slipNos) {
            List<PcSlipDetailSoleVo> list = pcSlipDetailSoleMapper.getSoleCheckDetail(slipNo, UserIdThread.get().getCompanyId());
            for (PcSlipDetailSoleVo pcSlipDetailSoleVo : list) {
                if ("2".equals(check)) {
                    if(!UserIdThread.get().getUserName().equals(pcSlipDetailSoleVo.getCfmUser())) throw new ServiceErrorException("不可取消检核，请确认是否为检核人");
                }
                if (ZStringUtils.isNotEmpty(pcSlipDetailSoleVo.getFinanceCfmFlag())) throw new ServiceErrorException("财务对账，无法修改");
                if (pcSlipDetailSoleVo.getPrice() == null) {
                    return ResponseInfo.error("单价不能为空");
                }
            }
        }
        pcSlipDetailSoleMapper.optionSoleCheck(slipNos, check, UserIdThread.get());
        return ResponseInfo.ok();
    }

    @Autowired
    SD_SQ_PcMtlCheckMapper checkMapper;

    @Override
    public ResponseInfo setRemarkSole(List<String> slipNos, String remark) {
        for (String slipNo : slipNos) {
            PcMtlCheck2 pcMtlCheck2 = new PcMtlCheck2();
            pcMtlCheck2.setRecNo(slipNo);
            pcMtlCheck2.setCompanyId(UserIdThread.get().getCompanyId());
            String str = checkMapper.getListByRecNo(pcMtlCheck2);
            if (str == null || "".equals(str)) {
                checkMapper.insertIntoRemark2(pcMtlCheck2, remark);
            } else {
                checkMapper.updateRemark2(pcMtlCheck2, remark);
            }
        }
        return ResponseInfo.ok();
    }

    @Override
    public ResponseInfo getSoleCheckDetail(String slipNo) {
        List<PcSlipDetailSoleVo> list = pcSlipDetailSoleMapper.getSoleCheckDetail(slipNo, UserIdThread.get().getCompanyId());
        DecimalFormat d1 = new DecimalFormat("0.00000");
        DecimalFormat d2 = new DecimalFormat("0.00");
        for (PcSlipDetailSoleVo pcSlipDetailSoleVo : list) {
            if (pcSlipDetailSoleVo.getPrice() != null)
                pcSlipDetailSoleVo.setPrices(d1.format(pcSlipDetailSoleVo.getPrice()));
            if (pcSlipDetailSoleVo.getAmt() != null) pcSlipDetailSoleVo.setAmts(d2.format(pcSlipDetailSoleVo.getAmt()));
        }
        return ResponseInfo.ok(list);
    }

    @Override
    public ResponseInfo saveSoleCheck(List<PcSlipDetailSoleVo> list) {
        pcSlipDetailSoleMapper.saveSoleCheck(list);
        return ResponseInfo.ok();
    }

    @Override
    public ResponseInfo setUnitPrice(List<PcSlipDetailSoleVo> list) {
        Integer i = 0;
        for (PcSlipDetailSoleVo detailPartBf2 : list) {
            if (detailPartBf2.getPrice() != null && detailPartBf2.getPrice().doubleValue() > 0) continue;
            BigDecimal unitPrice = pcSlipDetailSoleMapper.getPcUnitPrice(detailPartBf2);
            if (unitPrice != null) i++;
            unitPrice = unitPrice == null ? new BigDecimal(0) : unitPrice;
            BigDecimal qty = detailPartBf2.getInQty() == null ? new BigDecimal(0) : detailPartBf2.getInQty();
            BigDecimal amt = unitPrice.multiply(qty, MathContext.DECIMAL32);
            detailPartBf2.setAmts(String.valueOf(amt));
            detailPartBf2.setPrices(String.valueOf(unitPrice));
        }
        pcSlipDetailSoleMapper.saveSoleCheck(list);
        return ResponseInfo.ok(i);
    }

    @Override
    @Transactional
    public ResponseInfo refreshSoleCheck() {
        List<PcLotDetail> list = pcSlipDetailSoleMapper.getRefreshSoleCheck(UserIdThread.get().getCompanyId());
        Integer count = pcSlipDetailSoleMapper.refreshSoleCheck(UserIdThread.get().getCompanyId());
        if (ZStringUtils.isNotEmpty(list)) pcSlipDetailSoleMapper.updateSupply(list);
        return ResponseInfo.ok("刷新成功" + count + "条");
    }

    @Override
    public ResponseInfo deleteSoleCheck(List<String> slipNos) {
        for (String slipNo : slipNos) {
            List<PcSlipDetailSoleVo> list = pcSlipDetailSoleMapper.getSoleCheckDetail(slipNo, UserIdThread.get().getCompanyId());
            for (PcSlipDetailSoleVo vo : list) {
                if (ZStringUtils.isNotEmpty(vo.getFinanceCfmFlag())) throw new ServiceErrorException("财务对账，无法删除");
            }
        }
        int i = pcSlipDetailSoleMapper.deleteSoleCheck(slipNos, UserIdThread.get().getCompanyId());
        return ResponseInfo.ok("删除成功" + i + "条");
    }
}
